package hust.music.util;

import java.io.Serializable;

/**
 * Page
 * 
 * @author Txazo
 * @since 1.0
 */
public class Page implements Serializable {

	private static final long serialVersionUID = 1L;

	/** 当前页数 */
	private int pageNow;
	/** 每页显示记录数 */
	private int pageSize;
	/** 总记录数 */
	private int totalSize;
	/** 总页数 */
	private int totalPage;

	/** 是否存在上一页 */
	@SuppressWarnings("unused")
	private int hasPre;
	/** 是否存在下一页 */
	@SuppressWarnings("unused")
	private int hasNext;

	/** 页码 */
	@SuppressWarnings("unused")
	private int[] index;

	public Page(int pageNow, int pageSize, int totalSize) {
		this.pageNow = pageNow;
		this.pageSize = pageSize;
		this.totalSize = totalSize;
	}

	public int getPageNow() {
		return pageNow;
	}

	public void setPageNow(int pageNow) {
		this.pageNow = pageNow;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public int getTotalSize() {
		return totalSize;
	}

	public void setTotalSize(int totalSize) {
		this.totalSize = totalSize;
	}

	public int getTotalPage() {
		totalPage = this.getTotalSize() / this.getPageSize();
		if (this.getTotalSize() % this.getPageSize() != 0)
			totalPage++;
		return totalPage;
	}

	public void setTotalPage(int totalPage) {
		this.totalPage = totalPage;
	}

	public int getHasPre() {
		if (pageNow == 1) {
			return 0;
		}
		return 1;
	}

	public void setHasPre(int hasPre) {
		this.hasPre = hasPre;
	}

	public int getHasNext() {
		if (getTotalPage() == 1) {
			return 0;
		}
		if (pageNow == this.getTotalPage()) {
			return 0;
		}
		return 1;
	}

	public void setHasNext(int hasNext) {
		this.hasNext = hasNext;
	}

	public int[] getIndex() {
		int total = this.getTotalPage();
		if (total <= 9) {
			if (total == 1) {
				return new int[] {};
			}
			int[] index = new int[total];
			for (int i = 0; i < total; i++) {
				index[i] = i + 1;
			}
			return index;
		} else {
			if (pageNow <= 5) {
				return new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
			} else if (pageNow >= total - 4) {
				return new int[] { total - 8, total - 7, total - 6, total - 5,
						total - 4, total - 3, total - 2, total - 1, total };
			} else {
				return new int[] { pageNow - 4, pageNow - 3, pageNow - 2,
						pageNow - 1, pageNow, pageNow + 1, pageNow + 2,
						pageNow + 3, pageNow + 4 };
			}
		}
	}

	public void setIndex(int[] index) {
		this.index = index;
	}

}
